package com.eling.elcms.travel.service.impl;

import com.eling.elcms.core.AppContext;
import com.eling.elcms.core.service.impl.GenericManagerImpl;
import com.eling.elcms.core.util.PropertyUtils;
import com.eling.elcms.system.model.CommonUser;
import com.eling.elcms.travel.dao.IDinnerDao;
import com.eling.elcms.travel.model.CheckInDinner;
import com.eling.elcms.travel.model.CheckInImplement;
import com.eling.elcms.travel.model.CheckInImplement.CheckStatus;
import com.eling.elcms.travel.model.Dinner;
import com.eling.elcms.travel.service.ICheckInDinnerManager;
import com.eling.elcms.travel.service.ICheckInImplementManager;
import com.eling.elcms.travel.service.IDinnerManager;
import com.eling.elcms.travel.util.DateHandlerUtil;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class DinnerServiceImpl extends GenericManagerImpl<Dinner, Long> implements IDinnerManager {

	IDinnerDao dinnerDao;
	
	@Autowired
	private ICheckInDinnerManager checkInDinnerManager;
	@Autowired
	private ICheckInImplementManager checkInImplementManager;

	@Autowired
	public void setDao(IDinnerDao dao) {
		this.dao = dao;
		this.dinnerDao = dao;
	}

	@Override
	public CheckInDinner saveBatchDinner(Dinner dinner, Long pkCheckInImplement, Long pkCheckInDinner, Date startDate,
			Date endDate) {
		CheckInDinner checkInDinner = new CheckInDinner();
		CheckInImplement checkInImplement = checkInImplementManager.get(pkCheckInImplement);
		Set<Dinner> dinnerOldSet = new HashSet<>();
		List<Dinner> dinnerNewList = new ArrayList<>();
		Set<Dinner> removeSet = new HashSet<>();
		if(pkCheckInDinner != null){
			checkInDinner = checkInDinnerManager.get(pkCheckInDinner);
			dinnerOldSet = checkInDinner.getDinners();
			for(Dinner dn : dinnerOldSet){
				if(startDate != null && 
						dinner.getDinnerSort().equals(dn.getDinnerSort())
						&& dn.getDateOfDinner().getTime() >= startDate.getTime()
						&& dn.getDateOfDinner().getTime() <= endDate.getTime()){
					removeSet.add(dn);
				}else{
					dinnerNewList.add(dn);
				}
			}
		}
		checkInDinner.setOperateDate(new Date());
		checkInDinner.setOperator((CommonUser)AppContext.curUser());
		checkInDinner.setStatus(CheckStatus.Pending);
		checkInDinner = checkInDinnerManager.save(checkInDinner);
		
		if(endDate != null){
			Set<Date> dateSet = DateHandlerUtil.timeOfDaysByTwoDate(startDate, endDate);
			for(Date dt : dateSet){
				Dinner iDinner = new Dinner();
				PropertyUtils.copyProperties(iDinner, dinner);
				iDinner.setDateOfDinner(dt);
				iDinner.setCheckInDinner(checkInDinner);
				dinnerNewList.add(iDinner);
			}
		}else{
			//结束日期为空，表示为单条数据新增（非编辑）
			dinner.setCheckInDinner(checkInDinner);
			dinnerNewList.add(dinner);
		}
		dinnerNewList = save(dinnerNewList);
		checkInDinner.setDinners(new HashSet<>(dinnerNewList));
		checkInDinner = checkInDinnerManager.save(checkInDinner);
		
		// 旧的餐饮不为空且不是批量新增，则删除
		if(!CollectionUtils.isEmpty(removeSet)){
			remove(new ArrayList<>(removeSet));
		}
		
		checkInImplement.setCheckInDinner(checkInDinner);
		checkInImplementManager.save(checkInImplement);
		
		return checkInDinner;
	}

	@Override
	public CheckInDinner pendDinner(Long pkCheckInDinner, Long pkDinner) {
		CheckInDinner checkInDinner = checkInDinnerManager.get(pkCheckInDinner);
		// 单个落实
		Set<Dinner> dinnerSet = checkInDinner.getDinners();
		Boolean flag = true;
		if(pkDinner != null){
			Dinner dinner = get(pkDinner);
			dinner.setStatus(CheckStatus.Pended);
			dinner = save(dinner);
			
			for(Dinner dn : dinnerSet){
				// 查询其他餐饮是否落实
				if(dn.getStatus().equals(CheckStatus.Pending) && 
						!dn.getPkDinner().equals(pkDinner) && dn.getIsValid()){
					flag =  false;
				}
			}
		}else{
			// 全部落实
			for(Dinner dn : dinnerSet){
				dn.setStatus(CheckStatus.Pended);
			}
			save(new ArrayList<>(dinnerSet));
		}
		if(flag){
			//修改总餐饮为已落实状态
			checkInDinner.setStatus(CheckStatus.Pended);
			checkInDinner = checkInDinnerManager.save(checkInDinner);
			//判断和修改落实单状态
			checkInImplementManager.setCiiCheckStatusConfirm(checkInDinner.getCheckInImplement());
		}
		return checkInDinner;
	}
}
